<?php

defined('_SECURITY_TOKEN') or die("caia fora");

abstract class application extends framework{
		
	private static $outputview="";
	private static $outputHeaderCSS=array();
	private static $outputHeaderJS=array();
	
	private $paramerters_set="";
	
	
	
	
	
	public function __construct(){
		
		
		// try {
//         	
			// self::start_session( 'db' );
// 			
			// self::plugin();
// 		
			// self::module();
// 			
			// self::component();
// 			
			// self::template();
// 			
	    // }
	    // catch (AppException $e) {
	    	// throw new AppException( $e->getMessage() );
	        // //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    // }
	    // catch (Exception $e) {
	        // echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    // }
		
					
	}
	
	
	
	
	
	public static function instance(){
				
		try {
        	
			//self::$_values[ 'bruno' ] = 'iran'; 
			
			//if( empty($_REQUEST['parameters']) )
			//	exit();
			
			self::start_session( );
			
			$parameters 	=	application::getParameters();
			
			

			if(isset($parameters->lang)){
				
				switch($parameters->lang){
					case 'en':
						
						require_once( PATH_APPLICATION
										._SEPARATOR_SO
										.'language'
										._SEPARATOR_SO
										.'en'
										._SEPARATOR_SO
										.'application.php' );
										
						break;
					default:
		
						require_once( PATH_APPLICATION
										._SEPARATOR_SO
										.'language'
										._SEPARATOR_SO
										.'pt-br'
										._SEPARATOR_SO
										.'application.php' );
				}
				
			}else{
				
				require_once( PATH_APPLICATION
										._SEPARATOR_SO
										.'language'
										._SEPARATOR_SO
										.'pt-br'
										._SEPARATOR_SO
										.'application.php' );
										
			}
			
			
			if(defined('ACCESS_REGISTER_API') 
				|| defined('ACCESS_REGISTER_DOWNLOAD')){
				
				
				if(ACCESS_REGISTER_API==true 
					|| ACCESS_REGISTER_DOWNLOAD==true){
						
					$AcessStatistics=	application::instanceLibrary(
														'statistics',
														'access_statistics',
														'bm');		
				}
				
			
				if(ACCESS_REGISTER_API==true)
					$AcessStatistics->set_access_statistics_api($parameters);
							
				if(ACCESS_REGISTER_DOWNLOAD==true)
					if( $parameters->task == 'out' )
						$AcessStatistics->set_access_statistics_download(
																$parameters);
				
				
			}
			
			
			
			//var_dump( $parameters->component );exit();
			
			//var_dump( $_SESSION );
			$_REQUEST['component']=$parameters->component;
			$_REQUEST['controller']=$parameters->controller;
			$_REQUEST['task']=$parameters->task;
			
			
			//self::plugin();
		
			//self::module();
			
			self::component();
			//exit("p=".$_REQUEST['component']);
			self::template();
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	    //catch (Exception $e) {
	    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    //}
			
		
	}
	
	
	public static function getParameters(){
			
		$result='';
				
		try {
        	
			
			if( !empty($_REQUEST['component']))
				return $_REQUEST['parameters'];

			
			switch($_SERVER['REQUEST_METHOD']){
				case 'POST':
					
					break;
				case 'GET':
					
					break;
				case 'PUT':
					
					parse_str(file_get_contents("php://input"),$post_vars);
    				$_REQUEST = $post_vars;//echo $post_vars['fruit']." is the fruit\n";
	
					break;
				case 'DELETE':
					
					parse_str(file_get_contents("php://input"),$post_vars);
    				$_REQUEST = $post_vars;
					
					break;
			}
			
			//$file="/Applications/mappstack-5.4.22-0/apps/api/htdocs/apiopengovernmentdata/application/component/etl/etl.php";
			//require_once($file);
			
			//var_dump($_REQUEST['parameters']);
			//exit();
			
			if( empty($_REQUEST['parameters']) ){
					
					
				if( !empty($_REQUEST['key'])){
					
					if( empty($_REQUEST['notation']))
						$notation	=	"csv";
					else 
						$notation	=	$_REQUEST['notation'];
					
					if( empty($_REQUEST['download']))
						$download	=	"0";
					else 
						$download	=	$_REQUEST['download'];
					
					if( empty($_REQUEST['metadata']))
						$metadata	=	"0";
					else 
						$metadata	=	$_REQUEST['metadata'];
					
					if( empty($_REQUEST['limit']))
						$limit	=	"10";
					else 
						$limit	=	$_REQUEST['limit'];
					
					if( empty($_REQUEST['offset']))
						$offset	=	"0";
					else 
						$offset	=	$_REQUEST['offset'];
					
					
					$parameters=array("component"=>"etl",
										"controller"=>"view",
										"task"=>"out",
										"notation"=>$notation,
										"key"=>$_REQUEST['key'],
										"download"=>$download,
										"limit"=>$limit,
										"offset"=>$offset,
										"metadata"=>$metadata);
					
										
					//$_REQUEST['parameters'] = json_encode($parameters);
					$result = (object)  $parameters;
					
					$_REQUEST['notation']=$result->notation;
					$_REQUEST['download']=$result->download;
					$_REQUEST['metadata']=$result->metadata;
								
				}else	
					throw new AppException( "Sem parametros." );
				
			}else{
				$result 	=	json_decode($_REQUEST['parameters']);
			}	
			
			//var_dump( $result );exit();
			//$result 	=	json_decode($_REQUEST['parameters']);
			//$result	=	$result[0];
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	   // catch (Exception $e) {
	   //     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	   // }
		
		$_REQUEST['parameters']=$result;
		
		return $result;	
		
	}
	
	
	
	private static function plugin(){
		
	}
	
	private static function module(){
		
	}
	
	private static function component(){
			
		try {
        	
			$component	=	self::getComponent();

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.$component
							._SEPARATOR_SO
							.$component
							.'.php';
								
			if( file_exists( $filename ) ){
				
				require_once( PATH_APPLICATION
							._SEPARATOR_SO
							.'controller.class.php' );
					
				require_once( $filename );
					
			}else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
				
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
					
							
	}
	
	private static function template(){
	
		
		try {
        	
			$webservice=self::getWebservice();
		
			if( !empty($webservice) ){
			
				$template	=	self::getTemplate();
	
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."template"
								._SEPARATOR_SO
								.$template
								._SEPARATOR_SO
								.$template
								.'.php';
										
				if( file_exists( $filename ) ){
																	
					require_once( $filename );	
					
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
				
			}else{
					
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."template"
								._SEPARATOR_SO
								.'default'
								._SEPARATOR_SO
								.'webservice'
								.'.php';
										
				if( file_exists( $filename ) ){
																	
					require_once( $filename );	
					
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
				
			}
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	   // catch (Exception $e) {
	   //     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    //}
					
							
	}
	
	public static function getOutputController(){
		
		return self::$outputview;
	}
	
	public static function executeController(){

		
		try {
        						
			$controller	=	self::getController();
	
			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.self::getComponent()
							._SEPARATOR_SO
							.'controller'
							._SEPARATOR_SO
							.'controller'
							.self::getController()
							.'.class.php';
					
			
												
			if( file_exists( $filename ) ){
								
				require_once(  $filename );
				
				$controllername	=	'controller'
									.self::getController();
							
				$controller 	= new $controllername( );
				//exit("bruno=".$controllername);											
			}else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	   // catch (Exception $e) {
	    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    //}
		
									
	}
	
	
	public static function executeView( $view="", $args="" ){

		try {
        						
			if( !empty($view) ){
			
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."component"
								._SEPARATOR_SO
								.self::getComponent()
								._SEPARATOR_SO
								.'view'
								._SEPARATOR_SO
								.'view'
								.$view
								.'.class.php';
													
				if( file_exists( $filename ) ){
									
					require_once(  $filename );
					
					$viewname	=	'view'.$view;
					
					if( empty( $args ) )				
						$viewname	= new $viewname( );
					else 
						$viewname	= new $viewname( $args );
															
				}else
					throw new AppException( FILE_NOT_FOUND.': '.$filename );
			
			}else
				throw new AppException( VIEW_NOT_DEFINED.': '.$filename );
	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    //catch (Exception $e) {
	    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    //}
		
		
	}
	
	
	public static function setViewTmlp( $tmpl="default" ){

		try {
        									
				$filename	=	PATH_APPLICATION
								._SEPARATOR_SO
								."component"
								._SEPARATOR_SO
								.self::getComponent()
								._SEPARATOR_SO
								.'view'
								._SEPARATOR_SO
								.'tmpl'
								._SEPARATOR_SO
								.$tmpl
								.'.php';
													
				if( file_exists( $filename ) ){
					
					ob_start();
					require_once(  $filename );	
					
					self::$outputview = ob_get_contents();
										
					//ob_end_flush();	
					ob_end_clean();		
												 
																		
				}else
					throw new AppException( TMPL_NOT_DEFINED.': '.$filename );
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	    //catch (Exception $e) {
	    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	   // }
		
		
	}
	
	
	public static function getWebservice(){
		
		$result=NULL;
		
		try {
        	
			if( !empty($_REQUEST['webservice']) )
				if( $_REQUEST['webservice']!='' )
					$result	=	$_REQUEST['webservice'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	
	
	public static function getTemplate(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['tmpl']) ){
				
				if( empty( configuration::$template_default) )
					throw new AppException( TEMPLATE_NOT_DEFINED );
				else
					$result	=	configuration::$template_default;
			}
			else
				$result	=	$_REQUEST['tmpl'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	public static function getComponent(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['component']) )
				$result	=	'system';//throw new AppException( COMPONENT_NOT_DEFINED );
			else
				$result	=	$_REQUEST['component'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}
	
	
	public static function getController(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['controller']) )
				$result	=	'system';//throw new AppException( CONTROLLER_NOT_DEFINED );
			else
				$result	=	$_REQUEST['controller'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}

	
	public static function getTask(){
		
		$result='';
		
		try {
        	
			if( empty($_REQUEST['task']) )
				throw new AppException( TASK_NOT_DEFINED );
			else
				$result	=	$_REQUEST['task'];
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
				
	}



	public static function IsAuthenticationRequired( $required=FALSE, $redirect="" ){
		
		if( $required == TRUE){
			
			if( !self::getAuthentication() ){
				
				//$result	=	array("status"=>"restrict access");
				
				$result=array("status"=>0,
									"data"=>MSG_25_LOGINI_EXIGIDO,
									"notification"=>25);
									
				echo json_encode($result); 					
				exit();
			
			}
		}
				
	}
	
	public static function clearHeaderCSS(){
		
		try {
        	
			self::$outputHeaderCSS = array();
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
	}
	
	public static function addHeaderCSS( $url="", $rel="stylesheet"){
		
		try {
        	
			array_push( self::$outputHeaderCSS, array('url'=>$url,'rel'=>$rel));
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
		
	}
	
	public static function getHeaderCSS(){
			
		$result='';
		
		try {
        	
			foreach( self::$outputHeaderCSS as $key=>$item ){

				$result .= '<link rel="'.$item['rel'].'" href="'.$item['url'].'" type="text/css" />'."\n";
			
			}						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
		
	}
	
	
	public static function clearHeaderJS(){
		
		try {
        	
			self::$outputHeaderJS = array();
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
	}
	
	public static function addHeaderJS( $url=""){
		
		try {
        	
			array_push( self::$outputHeaderJS, array('url'=>$url));
						
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
		
		
	}
	
	public static function getHeaderJS(){
			
		$result='';
		
		try {
        	
			foreach( self::$outputHeaderJS as $key=>$item )				
				$result .= '<script src="'.$item['url'].'"></script>'."\n";
									
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
			
	    }
			
		return $result;
		
	}
	
	
	public static function getModel( $model="" ){
	
		$result	=	false;
		
		try {
        	
			$component	=	self::getComponent();

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							."component"
							._SEPARATOR_SO
							.$component
							._SEPARATOR_SO
							.'model'
							._SEPARATOR_SO
							.'model'
							.$model
							.'.class.php';
									
			if( file_exists( $filename ) ){
				
				self::instanceModel();
				
				require_once( $filename );
				
				$modelname	=	'model'.$model;
								
				$result	= new $modelname( );
					
			}	
			else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	   // catch (Exception $e) {
	   //     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    //}
			
					
		return $result;
									
	}
	
	
	private static function instanceModel( ){
		
		try {

			$filename	=	PATH_APPLICATION
							._SEPARATOR_SO
							.'model'
							.'.class.php';
									
			if( file_exists( $filename ) ){
				require_once( $filename );
			}	
			else
				throw new AppException( FILE_NOT_FOUND.': '.$filename );
	
			
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	   // catch (Exception $e) {
	   //     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	   // }
			
		
									
	}
	
	
	
}

?>